//通过vue-router插件实现模板路由配置
import { createRouter, createWebHistory } from 'vue-router'
import { constanRoute } from './constantRoutes.js'
import { useUserStore } from '~/utils/pinia/user.js'
import { ElMessage } from 'element-plus'
import whiteList from '~/utils/router/whiteList.js'
import { useBookMarkStore } from '~/utils/pinia/bookmark.js'

let router = createRouter({
  history: createWebHistory(),
  routes: constanRoute,
  scrollBehavior(to, from, savedPosition) {
    return {
      //return desired position}
      left: 0,
      top: 0
    }
  }
})

let userStore = null
let bookMarkStore = null

// 初始化全局数据
const initInfo = () => {
  // userStore.initUser();
  bookMarkStore = useBookMarkStore()
  bookMarkStore.tagId = null
  // treeStore.openTreeFlag = treeStore.openTreeIcon = false;
  // treeStore.treeDeviceId = null;
  // treeStore.osType = null
  // treeStore.showGroupFlag = false
  // treeStore.treeLinkedTableCategoryId = null
  // treeStore.getServerStrategyTypeList()
}
router.beforeEach((to, from, next) => {
  //   initInfo()
  if (!userStore) {
    userStore = useUserStore()
  }
  if (to.matched.length === 0) {
    //空路由跳转
    router.push({ name: 'layout' })
    ElMessage.error('页面不存在')
  } else if (whiteList.includes(to.path)) {
    next()
  } else {
    initInfo()
    next()
  }
})

export default router
